package main

import (
	"CodeCover/api"
	"CodeCover/internal/config"
	"CodeCover/internal/db"
	"CodeCover/internal/log"
	"flag"
	golog "log"
	"time"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

var configPath string

func main() {

	flag.Parse()

	flag.StringVar(&configPath, "conf", "config/dev.yaml", "config path, eg: -conf config/config.yaml")

	allConfig, err := config.LoadConfig(configPath)
	if err != nil {
		golog.Fatalf("Load Config Failed, config=%v, error:%v", allConfig, err)
	}

	log.InitLogger(allConfig)
	db.InitDB(allConfig)

	r := gin.Default()
	r.Use(cors.New(cors.Config{
		// AllowAllOrigins:  true,
		AllowOrigins:     []string{"*"},
		AllowMethods:     []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Content-Type", "Authorization", "Accept"},
		ExposeHeaders:    []string{"Content-Length", "Content-Type"},
		AllowCredentials: true,
		// AllowOriginFunc: func(origin string) bool {
		// 	// 生产环境建议替换为域名白名单校验
		// 	return true // 临时允许所有（测试用）
		// },
		MaxAge: 12 * time.Hour,
	}))

	api.StartRoute(r)
	api.IndexRoute(r)
	api.RegisterRoute(r)
	api.LoginRoute(r)
	api.TaskRoute(r)
	api.TokenRoute(r)
	r.Run(":8082")
}
